package com.xbongbong.saas.model.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.dao.PaasFormRichTextDao;
import com.xbongbong.paas.domain.entity.PaasFormRichTextEntity;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.pojo.dto.FormDataUpdateDTO;
import com.xbongbong.paas.pojo.dto.FormRichTextBatchDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.pro.enums.RichTextEnum;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.model.PaasFormRichTextModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * @author bomfish
 */
@Service("paasFormRichTextModel")
public class PaasFormRichTextModelImpl implements PaasFormRichTextModel {
    @Resource
    private PaasFormRichTextDao paasFormRichTextDao;
    private Object mongoLogHelp;

    @Override
    public Long insert(PaasFormRichTextEntity entity) {
        entity.setAddTime(DateTimeUtil.getInt());
        entity.setUpdateTime(DateTimeUtil.getInt());
        return paasFormRichTextDao.insert(entity);
    }

    @Override
    public void insertBatch(List<PaasFormRichTextEntity> list) {
        paasFormRichTextDao.insertBatch(list);
    }

    @Override
    public List<PaasFormRichTextEntity> findEntities(Map<String, Object> param) {
        return paasFormRichTextDao.findEntities(param);
    }

    @Override
    public Integer getEntityCount(Map<String, Object> param) {
        return paasFormRichTextDao.getEntitiesCount(param);
    }

    @Override
    public Integer update(PaasFormRichTextEntity entity) {
        entity.setUpdateTime(DateTimeUtil.getInt());
        return paasFormRichTextDao.update(entity);
    }

    @Override
    public Integer updateByDataId(PaasFormRichTextEntity entity) {
        Integer updateLine = paasFormRichTextDao.updateByDataId(entity);
        if (Objects.equals(updateLine, BasicConstant.ONE)) {
            entity.setUpdateTime(DateTimeUtil.getInt());
            this.updateTime(entity);
        }
        return updateLine;
    }

    @Override
    public PaasFormRichTextEntity getByKey(Long key, String corpid) {
        return paasFormRichTextDao.getByKey(key, corpid);
    }

    @Override
    public PaasFormRichTextEntity getByDataId(PaasFormRichTextEntity entity) {
        return paasFormRichTextDao.getByDataId(entity);
    }

    @Override
    public void deleteByDataId(PaasFormRichTextEntity entity) {
        entity.setUpdateTime(DateTimeUtil.getInt());
        paasFormRichTextDao.deleteByDataId(entity);
    }

    @Override
    public boolean hasData(Map<String, Object> params) {
        int size = paasFormRichTextDao.getIdList(params).size();
        return size != 0;
    }

    @Override
    public Integer countData(Map<String, Object> params) {
        return paasFormRichTextDao.getIdList(params).size();
    }

    @Override
    public void updateTime(PaasFormRichTextEntity entity) {
        paasFormRichTextDao.updateTime(entity);
    }

    @Override
    public void operateBatch(FormRichTextBatchDTO batchDTO) {
        batchDTO.setUpdateTime(DateTimeUtil.getInt());
        try {
            Map<String, Object> params = BeanUtil.convertBean2Map(batchDTO);
            paasFormRichTextDao.operateBatch(params);
        } catch (XbbException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void addRichTextByDataId(Long dataId, String corpid, Long formId, Integer businessType, Integer saasMark, Integer del, JSONObject data) {
        PaasFormRichTextEntity entity = getByDataId(dataId, corpid, formId, businessType, saasMark, del);
        if (Objects.nonNull(entity)) {
            data.put(RichTextEnum.RICH_TEXT.getAttr(), entity.getData());
        }
    }

    public PaasFormRichTextEntity getByDataId(Long dataId, String corpid, Long formId, Integer businessType, Integer saasMark, Integer del) {
        PaasFormRichTextEntity entity = new PaasFormRichTextEntity(dataId, corpid, formId, businessType, saasMark, del);
        return getByDataId(entity);
    }

    @Override
    public Integer updateFormRichText(FormDataUpdateDTO formDataUpdateDTO, Object richTextData) {
        if (Objects.isNull(richTextData)) {
            return 0;
        }
        Long dataId = formDataUpdateDTO.getDataId();
        PaasFormRichTextEntity oldEntity = this.getByDataId(dataId, formDataUpdateDTO.getCorpid(),
                formDataUpdateDTO.getFormId(), formDataUpdateDTO.getBusinessType(),
                formDataUpdateDTO.getSaasMark(), DelEnum.NORMAL.getDel());
        Integer updateRichTextLine = BasicConstant.ONE;
        if (Objects.isNull(oldEntity)) {
            PaasFormRichTextEntity entity = new PaasFormRichTextEntity();
            BeanUtil.copyProperties(formDataUpdateDTO, entity);
            entity.setOwnerId(Optional.ofNullable(entity.getOwnerId()).orElse(""));
            entity.setCreatorId(Optional.ofNullable(entity.getOwnerId()).orElse(""));
            entity.setDistributorMark(Optional.ofNullable(entity.getDistributorMark()).orElse(BasicConstant.ZERO));
            entity.setData(richTextData.toString());
            entity.setDel(DelEnum.NORMAL.getDel());
            insert(entity);
        } else {
            oldEntity.setData(richTextData.toString());
            updateRichTextLine = updateByDataId(oldEntity);
        }
        return updateRichTextLine;
    }


}
